# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: xguo, donggu

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


def generate():
  mea = 'Spot.Huobi.v1'
  me = 'Spot.Huobi'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get('https://api.huobi.pro/v1/settings/common/market-symbols').json()['data']

  pis = []
  for info in data:
    #  "online" - Listed, available for trading
    # "offline" - de-listed, not available for trading
    # "suspend" - suspended for trading, "pre-online" - to be online soon
    if info['state'] in ['offline']:
      continue

    native_symbol = info['symbol']
    native_base = info['bc']
    native_quote = info['qc']
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)
    
    price_precision = 10 ** info["pp"]
    amount_precision = 10 ** info['ap']

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=1/price_precision,
        qty_ticksize=1/amount_precision,
        min_qty=float(info['minoa']),
        min_amount=float(info['minov']),
    )
    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  symbol_list = [info['native_symbol'] for info in data]
  quote_currency_list = [info['native_quote'].upper() for info in data]
  base_currency_list = [info['native_base'].upper() for info in data]
  currency_list = base_currency_list + quote_currency_list
  res = {
      'symbol_list': list(set(symbol_list)),
      'quote_currency_list': list(set(quote_currency_list)),
      'currency_list': list(set(currency_list)),
  }
  return res


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
